<?php

namespace App\Models;

use App\Tools\JsonTools;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxUser extends Model
{
    const XUN=50;
    const CROSS=10;
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'wx_user';
    protected $guarded=[];
    protected $fillable = [
        'openid',
        'session_key',
        'user_phone',
        'user_password',
        'last_login',
        'last_login_mode',
        'user_name',
        'user_image'
    ];

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/10/20
     * @Time: 8:56
     * `user_integral` int(10) DEFAULT NULL COMMENT '积分总数',
     * `user_residue_integral` int(10) DEFAULT NULL COMMENT '剩余积分',
     * `user_user_integral` int(10) DEFAULT NULL COMMENT '使用积分',
     */
    public function  UserIntegral($uid,$integral){
        $userIntegral=$this->where("id",$uid)->first();
        if($userIntegral){
            \DB::beginTransaction();
            try {
                if($integral==self::XUN){//传入的积分为50
                    $IntegralUpdate=$userIntegral->update([
                        'user_integral'=>$userIntegral['user_integral']+self::XUN,
                        'user_residue_integral'=>$userIntegral['user_residue_integral']+self::XUN
                    ]);
                    PointsDetail::create([
                        'uid'=>$uid,
                        'points'=>$integral,
                        'points_type'=>1,
                        'points_mode'=>1
                    ]);
                    \DB::commit();
                    if(!$IntegralUpdate){
                        abort('20005','签到失败');
                    }
                    return  JsonTools::SuccessJson();
                }
                if($integral==self::CROSS){
                    $IntegralUpdate=$userIntegral->update([
                        'user_integral'=>$userIntegral['user_integral']+self::CROSS,
                        'user_residue_integral'=>$userIntegral['user_residue_integral']+self::CROSS
                    ]);
                    PointsDetail::create([
                        'uid'=>$uid,
                        'points'=>$integral,
                        'points_type'=>1,
                        'points_mode'=>1
                    ]);
                    \DB::commit();
                    if(!$IntegralUpdate){
                        abort('20005','签到失败');
                    }
                    return  JsonTools::SuccessJson();
                }
            }catch(\Exception $exception){
                \DB::rollBack();
                return  JsonTools::ErrorsJson([],$exception->getCode(),$exception->getMessage());
            }

        }else{
            return JsonTools::ErrorsJson([],2005,"查询不到此用户");
        }

    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/22,
     * @Time: 13:45,
     * 用户与经纪人聊天交流
     */
    public function message(){
        return self::hasMany(UserMessage::class,'uid','id');
    }
}
